#include "wikicustomdialog.h"
#include "ui_wikicustomdialog.h"
#include "wikiimportdatasetdialog.h"
#include "wikiexportdatasetdialog.h"
#include "superdatasetgeneratordialog.h"

#include <QMenu>
#include <QMessageBox>
#include <response.h>

#include <config/config.h>

#include <wiki/wiki.h>

WikiCustomDialog::WikiCustomDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::WikiCustomDialog)
{
    ui->setupUi(this);
    ui->splitter->setSizes(QList<int>() << 150 << 400);

    ui->comboBox->addItem("类型 Url", Wiki::Url);
    ui->comboBox->addItem("类型 Content", Wiki::Content);

    load_list_item();
}

WikiCustomDialog::~WikiCustomDialog()
{
    save_list_item();
    delete ui;
}

void WikiCustomDialog::on_listWidget_customContextMenuRequested(const QPoint &pos)
{
    QMenu menu;
    menu.addAction(ui->action_add_item);
    menu.addAction(ui->action_remove_item);

    menu.addSeparator();
    menu.addAction(ui->action_import_dataset);
    menu.addAction(ui->action_export_dataset);

    menu.addSeparator();
    menu.addAction(ui->action_export_select);

    ui->action_remove_item->setEnabled(ui->listWidget->currentItem()!=nullptr);
    ui->action_export_select->setEnabled(ui->listWidget->currentItem()!=nullptr);

    menu.exec(QCursor::pos());
}

void WikiCustomDialog::on_action_add_item_triggered()
{
    auto item = new QListWidgetItem;
    item->setText("新增类");
    item->setData(990, "");
    item->setData(991, "");
    ui->listWidget->addItem(item);
    ui->listWidget->setCurrentItem(item);
}

void WikiCustomDialog::on_action_remove_item_triggered()
{
    auto item = ui->listWidget->currentItem();
    if (item) {
        if (QMessageBox::Yes == QMessageBox::question(this, "确定删除", "确定要删除选中的'"+item->text()+"'吗?")) {
            ui->listWidget->takeItem(ui->listWidget->currentRow());
            ui->listWidget->setCurrentRow(0);
        }
    }
}

void WikiCustomDialog::on_listWidget_currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous)
{
    auto item = ui->listWidget->currentItem();

    if (item == nullptr) {
        return;
    }

    m_busy = true;

    ui->edit_name->setText(item->text());
    ui->edit_prefix->setText(item->data(990).toString());
    ui->plain_preview->setPlainText(item->data(991).toString());
    ui->plain_editor->setPlainText(item->data(991).toString());
    ui->plain_javascript->setPlainText(item->data(992).toString());
    ui->plain_blockurls->setPlainText(item->data(993).toString());
    ui->plain_comment->setPlainText(item->data(994).toString());

    m_busy = false;
}

void WikiCustomDialog::on_edit_name_textChanged(const QString &arg1)
{
    auto item = ui->listWidget->currentItem();

    if (item == nullptr || m_busy) {
        return;
    }

    item->setText(arg1);
}

void WikiCustomDialog::on_edit_prefix_textChanged(const QString &arg1)
{
    auto item = ui->listWidget->currentItem();

    if (item == nullptr || m_busy) {
        return;
    }
    item->setData(990, arg1);
}

void WikiCustomDialog::on_plain_editor_textChanged()
{
    ui->label_count->setText(QString("%1条").arg(ui->plain_editor->toPlainText().split("\n").count()));
    auto item = ui->listWidget->currentItem();

    if (item == nullptr || m_busy) {
        return;
    }
    item->setData(991, ui->plain_editor->toPlainText());
}

void WikiCustomDialog::on_plain_javascript_textChanged()
{
    auto item = ui->listWidget->currentItem();

    if (item == nullptr || m_busy) {
        return;
    }
    item->setData(992, ui->plain_javascript->toPlainText());
}

void WikiCustomDialog::on_plain_blockurls_textChanged()
{
    auto item = ui->listWidget->currentItem();

    if (item == nullptr || m_busy) {
        return;
    }
    item->setData(993, ui->plain_blockurls->toPlainText());
}

void WikiCustomDialog::on_plain_comment_textChanged()
{
    auto item = ui->listWidget->currentItem();

    if (item == nullptr || m_busy) {
        return;
    }
    item->setData(994, ui->plain_comment->toPlainText());
}

QListWidgetItem* WikiCustomDialog::vmap_to_item(QVariantMap map)
{
    auto item = new QListWidgetItem;
    item->setData(Qt::UserRole, map);
    item->setText(map.value("title").toString());
    item->setData(990, map.value("prefix").toString());
    item->setData(991, map.value("content").toString());
    item->setData(992, map.value("javascript").toString());
    item->setData(993, map.value("blockurls").toString());
    item->setData(994, map.value("comment").toString());
    return item;
}

void WikiCustomDialog::load_list_item()
{
    auto list = Response(0, Config::getAppDataConfig()).toList();
    for (int row = 0; row < list.count(); ++row) {
        QVariantMap map = list.at(row).toMap();

        auto item = vmap_to_item(map);
        ui->listWidget->addItem(item);
    }
}

QVariantMap WikiCustomDialog::item_to_map(const QListWidgetItem *item)
{
    QVariantMap map = item->data(Qt::UserRole).toMap();
    map.insert("title", item->text());
    map.insert("prefix", item->data(990).toString());
    map.insert("content", item->data(991).toString());
    map.insert("javascript", item->data(992).toString());
    map.insert("blockurls", item->data(993).toString());
    map.insert("comment", item->data(994).toString());
    return map;
}

QVariantList WikiCustomDialog::list_item_to_vlist()
{
    QVariantList list;
    int rows = ui->listWidget->count();
    for (int row = 0; row < rows; ++row) {
        auto item = ui->listWidget->item(row);
        QVariantMap map = item_to_map(item);
        list << map;
    }

    return list;
}

void WikiCustomDialog::save_list_item()
{
    QVariantList list = list_item_to_vlist();
    QJsonDocument document = QJsonDocument::fromVariant(list);
    Config::setAppDataConfig(document.toJson());
}

/**
 * @brief 导入数据集
 */
void WikiCustomDialog::on_action_import_dataset_triggered()
{
    WikiImportDataSetDialog dialog;
    dialog.setWindowTitle("导入数据集");
    if (dialog.exec() == QDialog::Accepted) {
        auto list = dialog.dataset();
        for (int row = 0; row < list.count(); ++row) {
            QVariantMap map = list.at(row).toMap();

            auto item = vmap_to_item(map);
            ui->listWidget->addItem(item);
        }
    }
}

/**
 * @brief 导出数据集
 */
void WikiCustomDialog::on_action_export_dataset_triggered()
{
    QVariantList list = list_item_to_vlist();
    QJsonDocument document = QJsonDocument::fromVariant(list);

    WikiExportDataSetDialog dialog;
    dialog.setWindowTitle("导出数据集");
    dialog.setData(document.toJson(QJsonDocument::Indented));
    if (dialog.exec() == QDialog::Accepted) {

    }
}

/**
 * @brief 导出指定数据集
 */
void WikiCustomDialog::on_action_export_select_triggered()
{
    auto item = ui->listWidget->currentItem();

    QVariantMap map = item_to_map(item);
    QJsonDocument document = QJsonDocument::fromVariant(map);

    WikiExportDataSetDialog dialog;
    dialog.setWindowTitle("导出指定数据集");
    dialog.setTitle(item->text());
    dialog.setData(document.toJson(QJsonDocument::Indented));
    if (dialog.exec() == QDialog::Accepted) {

    }
}

void WikiCustomDialog::on_btn_super_clicked()
{
    SuperDatasetGeneratorDialog dialog;
    dialog.setOutputs(ui->plain_editor->toPlainText());
    if (dialog.exec() == QDialog::Accepted) {
        ui->plain_editor->setPlainText(dialog.outputs());
    }
}

